package com.tjsinfo.tjsoa.documentationflow.dangweishouwen.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.documentationflow.dangweishouwen.bo.ITDangweishouwenFujianBO;
import com.tjsinfo.tjsoa.documentationflow.dangweishouwen.bo.ITDangweishouwenNeirongBO;
import com.tjsinfo.tjsoa.documentationflow.dangweishouwen.vo.TDangweishouwenFujian;
import com.tjsinfo.tjsoa.documentationflow.dangweishouwen.vo.TDangweishouwenNeirong;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

public class ZhuanHuanDengJiFromAction  extends Action{
	private ISystemUserBo systemUserBo;
	private ITDangweishouwenNeirongBO dangweishouwenNeirongBO;
	private ITDangweishouwenFujianBO dangweishouwenFujianBO;
	
	

	public ITDangweishouwenNeirongBO getDangweishouwenNeirongBO() {
		return dangweishouwenNeirongBO;
	}
	public void setDangweishouwenNeirongBO(
			ITDangweishouwenNeirongBO dangweishouwenNeirongBO) {
		this.dangweishouwenNeirongBO = dangweishouwenNeirongBO;
	}
	
	
	public ITDangweishouwenFujianBO getDangweishouwenFujianBO() {
		return dangweishouwenFujianBO;
	}
	public void setDangweishouwenFujianBO(
			ITDangweishouwenFujianBO dangweishouwenFujianBO) {
		this.dangweishouwenFujianBO = dangweishouwenFujianBO;
	}
	public ISystemUserBo getSystemUserBo() {
		return systemUserBo;
	}
	public void setSystemUserBo(ISystemUserBo systemUserBo) {
		this.systemUserBo = systemUserBo;
	}
	
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
				
		String fileurl = ToolKit.getProperties("com/tjsinfo/tjsoa/documentationflow/dangweishouwen/properties/systemResource_zh_CN.properties", this).getProperty("dangweishouwen.fileurl"); 
		String qingban = ToolKit.getProperties("com/tjsinfo/tjsoa/documentationflow/dangweishouwen/properties/systemResource_zh_CN.properties", this).getProperty("dangweishouwen.qingban");
		List<TSystemUser> systemuserlist = systemUserBo.selectByDutyName(qingban);
		if (systemuserlist != null &&systemuserlist.size() > 0) {
			TSystemUser systemUser = systemuserlist.get(0);
			request.setAttribute("qingbanname", systemUser.getUserName());
			request.setAttribute("qingbanid", systemUser.getUserId());
		
		} else {
			request.setAttribute("qingbanname", "admin");
			request.setAttribute("qingbanid", 1);
		}
		Integer zongshouwenbianhao = dangweishouwenNeirongBO.selectMaxZongShouWenHaoByYear();
		if(zongshouwenbianhao==0)
		{
			zongshouwenbianhao=1;
		}
		if(zongshouwenbianhao != 1){
			request.setAttribute("zongshouwenbianhao", (new Date().getYear()+ 1900)+zongshouwenbianhao.toString().substring(4));
		}else{
			request.setAttribute("zongshouwenbianhao", (new Date().getYear()+ 1900)+"0001");
		}
		
		Integer dataBaseId = Integer.parseInt(request.getParameter("dataBaseId"));
		String id = request.getParameter("id");
		TDangweishouwenNeirong dangweishouwenNeirong =   dangweishouwenNeirongBO.selectDownDangWeishouwenNeirongById(dataBaseId, id);
		List<TDangweishouwenFujian> list =  dangweishouwenNeirongBO.selectDownDangWeishouwenFuJian(dataBaseId, id);
		request.setAttribute("dangweishouwenNeirong", dangweishouwenNeirong);
				
		ToolKit.copyFile(request.getSession().getServletContext().getRealPath("/") + "\\"+ "documentationflow\\dangweifawen\\docs\\"+dangweishouwenNeirong.getDocId(), fileurl+"\\"+dangweishouwenNeirong.getDocId());
		for (TDangweishouwenFujian dangweishouwenFujian : list) {
			ToolKit.copyFile(request.getSession().getServletContext().getRealPath("/") + "\\"+ "documentationflow\\dangweifawen\\docs\\"+dangweishouwenFujian.getFujianZhengname(), fileurl+"\\"+dangweishouwenFujian.getFujianZhengname());
			dangweishouwenFujian.setFujianAddress("D:\\gongwen\\dangweishouwen\\"+dangweishouwenFujian.getFujianZhengname());
			TDangweishouwenNeirong neirong = new TDangweishouwenNeirong();
			neirong.setId("0");
			dangweishouwenFujian.setTDangweishouwenNeirong(neirong);
			String fujianid =  dangweishouwenFujianBO.addFujianId(dangweishouwenFujian);
			dangweishouwenFujian.setId(fujianid);
		}
		TDangweishouwenFujian dangweishouwenFujian = new TDangweishouwenFujian();
		dangweishouwenFujian.setFujianName(dangweishouwenNeirong.getZhaiyou());
		dangweishouwenFujian.setFujianZhengname(dangweishouwenNeirong.getDocId());
		dangweishouwenFujian.setFujianAddress("D:\\gongwen\\dangweishouwen\\"+dangweishouwenFujian.getFujianZhengname());
		TDangweishouwenNeirong neirong = new TDangweishouwenNeirong();
		neirong.setId("0");
		dangweishouwenFujian.setTDangweishouwenNeirong(neirong);
		String fujianid =  dangweishouwenFujianBO.addFujianId(dangweishouwenFujian);
		dangweishouwenFujian.setId(fujianid);
		list.add(0, dangweishouwenFujian);
		request.getSession().setAttribute("dangweishouwenNeirongFuJianList", list);
		String taskId = request.getParameter("taskId");
		request.setAttribute("taskId", taskId);
		request.setAttribute("dataBaseId", dataBaseId);		
		return mapping.findForward("ok");
	}
	
}

